//
//  KImageLayer.m
//  ToneMapper
//
//  Created by Andrey on 29/04/2009.
//  Copyright 2009 Karma Software. All rights reserved.
//

#import "KImageLayer.h"


@implementation KImageLayer

@synthesize width;
@synthesize height;
@synthesize name;
@synthesize opacity;
@synthesize visible;
@synthesize blendMode;

- (id) init
{
	if (self = [super init])
	{
		width = height = 0;
		pixels = nil;
		name = @"New layer";
		opacity = 1.0;
		visible = NO;
		blendMode = NormalBlendMode;
	}
	
	return self;
}

- (id) initWithWidth: (NSUInteger)aWidth andHeight: (NSUInteger)aHeight
{
	if (self = [super init])
	{
		width = aWidth;
		height = aHeight;

		pixels = (KPixel**)calloc(width * height, sizeof(KPixel*));
		
		name = @"New layer";
		opacity = 1.0;
		visible = YES;
		blendMode = NormalBlendMode;
	}
	
	return self;
}

- (KPixel*) pixelAtX: (NSUInteger)x y: (NSUInteger)y
{
	NSUInteger index = y * width + x;
	
	return pixels[index];
}

- (void) setPixel: (KPixel*)pixel atX: (NSUInteger)x y: (NSUInteger)y
{
	NSUInteger index = y * width + x;
	
	[pixels[index] release];
	
	pixels[index] = [pixel retain];
}

- (void) dealloc
{
	for (int i = 0; i < width * height; i++)
	{
		[pixels[i] release];
	}
	
	free(pixels);
	[super dealloc];
}

@end
